const ProductConfig = require('../models/ProductConfig');

async function initProductConfigs() {
  const configs = [
    // 购买100个代币
    {
      stripe_price_id: "price_1RRq69EHIcYxLscxYXLxgJsn",
      product_type: "payment",
      name: "100代币",
      tokens_amount: 100
    },
    // 购买200个代币
    {
      stripe_price_id: "price_1RfxuLEHIcYxLscx49NXHhdU",
      product_type: "payment",
      name: "200代币",
      tokens_amount: 200
    }
    // 未来如果要添加订阅会员，可以在这里添加：
    // {
    //   stripe_price_id: "price_你的订阅价格ID",
    //   product_type: "subscription",
    //   name: "月度会员",
    //   tokens_amount: 100 // 每月发放100代币
    // }
  ];

  for (const config of configs) {
    try {
      const [productConfig, created] = await ProductConfig.findOrCreate({
        where: { stripe_price_id: config.stripe_price_id },
        defaults: config
      });
      
      if (created) {
        console.log(`创建产品配置: ${config.name} (${config.stripe_price_id})`);
      } else {
        console.log(`产品配置已存在: ${config.name} (${config.stripe_price_id})`);
      }
    } catch (error) {
      console.error(`创建产品配置失败 ${config.stripe_price_id}:`, error.message);
    }
  }
  
  console.log("产品配置初始化完成");
}

if (require.main === module) {
  // 直接运行此脚本时执行
  const { testConnection } = require('../config/database');
  
  (async () => {
    try {
      await testConnection();
      await initProductConfigs();
      process.exit(0);
    } catch (error) {
      console.error('初始化失败:', error);
      process.exit(1);
    }
  })();
}

module.exports = initProductConfigs; 